<?php
namespace App\Http\Controllers;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\WaterModule;
use App\Http\Controllers\Business\DataModule;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;

class DataController extends WebController
{
    //肿瘤医院总表
    public function cancerHospitalTotalWaterMeter() {
        $id = 1;
        $waterMeterInfoResult = WaterModule::getWaterMeter($id);
        if($waterMeterInfoResult['status'] == 'failed') {
            return view('errors.404');
        }
        $waterMeterInfo = $waterMeterInfoResult['data'];
        $waterMeterRadixListsResult = WaterModule::waterMeterDayConsumptionList($id);
        if($waterMeterRadixListsResult['status'] == 'success') {
            $waterMeterRadixData =  $waterMeterRadixListsResult['data'];
        } else {
            $waterMeterRadixData = [];
        }
        $data = [
            'id' => $id,
            'watermeter_info' => $waterMeterInfo,
            'watermeter_radix_data' => $waterMeterRadixData
        ];
        return view('data.cancer_hospital_total_watermeter', $data);
    }
    //肿瘤医院分表
    public function cancerHospitalChildWaterMeter() {
        return view('data.cancer_hospital_child_watermeter');
    }
    //肿瘤医院水平衡分析
    public function cancerHospitalBalanceWater() {
        $result = DataModule::fatherNodeList();
        $fatherNodeList = $result['data'];
        $data = [
            'father_node_list' => $fatherNodeList
        ];
        return view('data.cancer_hospital_water_balance', $data);
    }
    public function cancerHospitalWaterComparison() {
        $enterpriseName = "江西省肿瘤医院";
        $result = DataModule::waterMeterListByEnterpriseName($enterpriseName);
        if($result['status'] == 'failed') {
            return view('errors.404');
        } 
        $waterMeterlists = $result['data'];
        $data = [
            'water_meter_lists' => $waterMeterlists 
        ];
        return view('data.cancer_hospital_water_comparsion', $data);    
    }
    //两个水表比较
    public function twoWaterMeterComparison() {
        $waterMeterId1 = $this->request->get('water_meter_id1');
        $waterMeterTime1 = $this->request->get('water_meter_time1');
        $waterMeterId2 = $this->request->get('water_meter_id2');
        $waterMeterTime2 = $this->request->get('water_meter_time2');
        $result = DataModule::twoWaterMeterComparison($waterMeterId1, $waterMeterTime1, $waterMeterId2, $waterMeterTime2);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
    public function balanceAnalyze() {
        $number = $this->request->get('number');
        $date = $this->request->get('date');
        $basicInfoResult = DataModule::getDetailByWaterMeterNumber($number);
        $basicInfo = $basicInfoResult['data'];
        $balanceAnalyzeResult = DataModule::balanceAnalyze($number, $date);
        if($balanceAnalyzeResult['status'] == 'success') {
            $balanceAnalyze = $balanceAnalyzeResult['data'];
        } else {
            return $this->responseError($balanceAnalyzeResult['message']);
        }
        $data = [
            'date' => $date,
            'basic_info' => $basicInfo,
            'father_consumption' => $balanceAnalyze['father_consumption'],
            'child_lists' => $balanceAnalyze['child_lists'],
            'child_total_consumption' => $balanceAnalyze['child_total_consumption'],
            'loss_rate' => $balanceAnalyze['loss_rate'],
            'count' =>  $balanceAnalyze['count'],
            'valid_count' => $balanceAnalyze['valid_count'],
            'invalid_count' => $balanceAnalyze['invalid_count']
        ];
        return $this->responseSuccessData($data);
    }
    //不平衡原因
    public function notBalanceReason() {
        $number = $this->request->get('number');
        $date = $this->request->get('date');
        $result = DataModule::notBalanceReason($number, $date);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        } else {
            return $this->responseSuccessData($result['data']);
        }
    }
}